package com.example.metaactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            ActivityInfo act = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            Bundle metaData = act.metaData;
            Log.d(TAG, "onCreate: " + metaData.getString("weather"));
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

        View v = findViewById(R.id.v_content);
        v.setBackgroundResource(R.drawable.shape_rect_gold);


        Button btn = findViewById(R.id.btn1);
        btn.setBackgroundResource(R.drawable.shape_rect_gold);
        btn.setOnClickListener(item -> {
            Intent intent = new Intent(MainActivity.this, EditFocusActivity.class);
            startActivity(intent);
        });

        Button popUpDialog = findViewById(R.id.popup_dialog);
        popUpDialog.setOnClickListener(
                item -> {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.setTitle("尊敬的用户")
                            .setIcon(R.mipmap.ic_launcher)
                            .setMessage("要卸载吗")
                            .setPositiveButton("残忍拒绝", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(MainActivity.this, "离开啦", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(MainActivity.this, "让我再陪伴你365个日日夜夜", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .create();
                    alert.show();
                }
        );

        Button dateTimePicker = findViewById(R.id.datetime_picker);
        dateTimePicker.setOnClickListener(
                item -> {
                    Calendar calendar = Calendar.getInstance();
                    new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            String date = String.format("%d年%d月%d日", year, month + 1, dayOfMonth);
                            Toast.makeText(MainActivity.this, "您选择的日期是" + date, Toast.LENGTH_SHORT).show();
                        }
                    },
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH)
                    ).show();
                }
        );
    }
}